package com.mars.module.admin.controller;


import com.mars.common.enums.BusinessType;
import com.mars.common.response.PageInfo;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import com.mars.framework.annotation.Log;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.SysHouseRentRecord;
import com.mars.module.admin.request.SysHouseRentRecordRequest;
import com.mars.module.admin.service.ISysHouseRentRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 租房记录控制层
 *
 * @author mars
 * @date 2024-01-23
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "租房项目-租房记录接口管理",tags = "租房项目-租房记录接口管理")
@RequestMapping("/admin/sysHouseRentRecord" )
public class SysHouseRentRecordController {

    private final ISysHouseRentRecordService iSysHouseRentRecordService;

    /**
     * 分页查询租房记录列表
     */
    @ApiOperation(value = "分页查询租房记录列表")
    @PostMapping("/pageList")
    public R<PageInfo<SysHouseRentRecord>> pageList(@RequestBody SysHouseRentRecordRequest sysHouseRentRecordRequest) {
        return R.success(iSysHouseRentRecordService.pageList(sysHouseRentRecordRequest));
    }

    /**
     * 获取租房记录详细信息
     */
    @ApiOperation(value = "获取租房记录详细信息")
    @GetMapping(value = "/query/{id}")
    public R<SysHouseRentRecord> detail(@PathVariable("id") Long id) {
        return R.success(iSysHouseRentRecordService.getById(id));
    }

    /**
     * 新增租房记录
     */
    @Log(title = "新增租房记录", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增租房记录")
    @PostMapping("/add")
    public R<Void> add(@RequestBody SysHouseRentRecordRequest sysHouseRentRecordRequest) {
        iSysHouseRentRecordService.add(sysHouseRentRecordRequest);
        return R.success();
    }

    /**
     * 修改租房记录
     */
    @Log(title = "修改租房记录", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改租房记录")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody SysHouseRentRecordRequest sysHouseRentRecordRequest) {
        iSysHouseRentRecordService.update(sysHouseRentRecordRequest);
        return R.success();
    }

    /**
     * 删除租房记录
     */
    @Log(title = "删除租房记录", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除租房记录")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iSysHouseRentRecordService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出租房记录
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody SysHouseRentRecordRequest sysHouseRentRecordRequest) throws IOException {
        List<SysHouseRentRecord> list = iSysHouseRentRecordService.list(sysHouseRentRecordRequest);
        ExcelUtils.exportExcel(list, SysHouseRentRecord.class, "租房记录信息", response);
    }
}
